package com.xingdata.jjxc.m.avt;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.R.string;
import com.xingdata.jjxc.base.BaseActivity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.ExitApplication;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.SystemInfo;

public class EditLovecar extends BaseActivity implements OnClickListener {

	private TextView icon_tv, edit_car_plate_tv;
	private ImageView edit_car_plate_iv;

	/**
	 * 车牌号，发动机，hud序列号，底盘号，车架号，车名称，车厢类型
	 */
	private EditText edit_car_input_carnum_tv, edit_car_input_motor_tv,
			edit_car_input_hudnum_tv, edit_car_input_batholith_tv,
			edit_car_input_frame_tv, edit_car_from_tv, edit_car_state_tv;

	private Dialog edit_bus_dialog;
	/**
	 * 简介，hud序列号，底盘号，发动机号，车架号，车名，车样式
	 */
	private String car_id, abbreviate, hud_num, natholith, car_num,
			engine_number, frame_number, car_name, car_state;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		ExitApplication.getInstance().addActivity(this);
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_edit_lovecar;
	}

	@Override
	protected String getTitleText() {
		return JUtils.TITILE_EDITLOVECAR;
	}

	@Override
	protected void initViews() {
//		icon_tv = (TextView) findViewById(R.id.icon_tv);
//		icon_tv.setText(string.add_lovecar_save);
		icon_tv.setOnClickListener(this);
		edit_car_plate_iv = (ImageView) findViewById(R.id.edit_car_plate_iv);
		edit_car_plate_iv.setOnClickListener(this);
		edit_car_plate_tv = (TextView) findViewById(R.id.edit_car_plate_tv);

		edit_car_input_carnum_tv = (EditText) findViewById(R.id.edit_car_input_carnum_tv);
		edit_car_input_motor_tv = (EditText) findViewById(R.id.edit_car_input_motor_tv);
		edit_car_input_hudnum_tv = (EditText) findViewById(R.id.edit_car_input_hudnum_tv);
		edit_car_input_batholith_tv = (EditText) findViewById(R.id.edit_car_input_batholith_tv);
		edit_car_input_frame_tv = (EditText) findViewById(R.id.edit_car_input_frame_tv);
		edit_car_from_tv = (EditText) findViewById(R.id.edit_car_from_tv);
		edit_car_state_tv = (EditText) findViewById(R.id.edit_car_state_tv);

		edit_car_plate_tv.setText(abbreviate);
		edit_car_input_carnum_tv.setText(car_num);
		edit_car_input_motor_tv.setText(engine_number);
		edit_car_input_hudnum_tv.setText(hud_num);
		edit_car_input_batholith_tv.setText(natholith);
		edit_car_input_frame_tv.setText(frame_number);
		edit_car_from_tv.setText(car_name);
		edit_car_state_tv.setText(car_state);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
//		case R.id.icon_tv:
//			abbreviate = edit_car_plate_tv.getText().toString();
//			car_num = edit_car_input_carnum_tv.getText().toString();
//			engine_number = edit_car_input_motor_tv.getText().toString();
//			hud_num = edit_car_input_hudnum_tv.getText().toString();
//			natholith = edit_car_input_batholith_tv.getText().toString();
//			frame_number = edit_car_input_frame_tv.getText().toString();
//			car_name = edit_car_from_tv.getText().toString();
//			car_state = edit_car_state_tv.getText().toString();
//
//			if ("".equals(abbreviate)) {
//				showToast("请选择城市简称");
//			} else if ("".equals(car_num)) {
//				showToast("请输入车牌后六位");
//			} else if ("".equals(engine_number)) {
//				showToast("请输入完整发动机号");
//			} else if ("".equals(hud_num)) {
//				showToast("请输入hud序列号");
//			} else if ("".equals(natholith)) {
//				showToast("请输入底盘号");
//			} else if ("".equals(frame_number)) {
//				showToast("请输入完整车架号");
//			} else if ("".equals(car_name)) {
//				showToast("请输入汽车名称");
//			} else if ("".equals(car_state)) {
//				showToast("请输入车厢样式");
//			} else {
//				doPost_editLoveBus(car_id, hud_num, car_num, engine_number,
//						natholith);
//			}
//
//			break;
		case R.id.edit_car_plate_iv:
			showPicDialog();
			break;

		default:
			break;
		}

	}

	@Override
	protected void initDateBeforSetContentView() {
		Bundle bundle = getIntent().getExtras();
		car_id = bundle.getString("car_id");
		abbreviate = bundle.getString("abbreviate");
		hud_num = bundle.getString("hud_num");
		natholith = bundle.getString("natholith");
		car_num = bundle.getString("car_num");
		engine_number = bundle.getString("engine_number");
		frame_number = bundle.getString("frame_number");
		car_name = bundle.getString("car_name");
		car_state = bundle.getString("car_state");
	}

	private void showPicDialog() {

		if (edit_bus_dialog == null) {
			edit_bus_dialog = new Dialog(EditLovecar.this,
					R.style.popup_fade_anim);

			// 设置显示动画
			Window window = edit_bus_dialog.getWindow();
			window.setWindowAnimations(R.style.popup_fade_anim);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = getWindowManager().getDefaultDisplay().getHeight();
			window.setAttributes(wl);
			edit_bus_dialog.onWindowAttributesChanged(wl);
			edit_bus_dialog.setCanceledOnTouchOutside(true);
			View view = LayoutInflater.from(EditLovecar.this).inflate(
					R.layout.dialog_add_bus_capital, null);
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			edit_bus_dialog.addContentView(view, params);

			View blank = view.findViewById(R.id.dialog_love_bus_blackview);
			blank.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
				}
			});
			final TextView my_car_casital_tv_11, my_car_casital_tv_12, my_car_casital_tv_13, my_car_casital_tv_14, my_car_casital_tv_15, my_car_casital_tv_16, my_car_casital_tv_17, my_car_casital_tv_21, my_car_casital_tv_22, my_car_casital_tv_23, my_car_casital_tv_24, my_car_casital_tv_25, my_car_casital_tv_26, my_car_casital_tv_27, my_car_casital_tv_28, my_car_casital_tv_31, my_car_casital_tv_32, my_car_casital_tv_33, my_car_casital_tv_34, my_car_casital_tv_35, my_car_casital_tv_36, my_car_casital_tv_37, my_car_casital_tv_38, my_car_casital_tv_39, my_car_casital_tv_41, my_car_casital_tv_42, my_car_casital_tv_43, my_car_casital_tv_44, my_car_casital_tv_45, my_car_casital_tv_46, my_car_casital_tv_47, my_car_casital_tv_48, my_car_casital_tv_49, my_car_casital_tv_50;

			my_car_casital_tv_11 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_11);
			my_car_casital_tv_12 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_12);
			my_car_casital_tv_13 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_13);
			my_car_casital_tv_14 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_14);
			my_car_casital_tv_15 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_15);
			my_car_casital_tv_16 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_16);
			my_car_casital_tv_17 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_17);

			my_car_casital_tv_21 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_21);
			my_car_casital_tv_22 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_22);
			my_car_casital_tv_23 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_23);
			my_car_casital_tv_24 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_24);
			my_car_casital_tv_25 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_25);
			my_car_casital_tv_26 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_26);
			my_car_casital_tv_27 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_27);
			my_car_casital_tv_28 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_28);

			my_car_casital_tv_31 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_31);
			my_car_casital_tv_32 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_32);
			my_car_casital_tv_33 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_33);
			my_car_casital_tv_34 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_34);
			my_car_casital_tv_35 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_35);
			my_car_casital_tv_36 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_36);
			my_car_casital_tv_37 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_37);
			my_car_casital_tv_38 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_38);
			my_car_casital_tv_39 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_39);

			my_car_casital_tv_41 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_41);
			my_car_casital_tv_42 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_42);
			my_car_casital_tv_43 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_43);
			my_car_casital_tv_44 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_44);
			my_car_casital_tv_45 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_45);
			my_car_casital_tv_46 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_46);
			my_car_casital_tv_47 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_47);
			my_car_casital_tv_48 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_48);
			my_car_casital_tv_49 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_49);
			my_car_casital_tv_50 = (TextView) view
					.findViewById(R.id.my_car_casital_tv_50);

			my_car_casital_tv_11.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_11.getText()
							.toString());

				}
			});
			my_car_casital_tv_12.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_12.getText()
							.toString());

				}
			});
			my_car_casital_tv_13.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_13.getText()
							.toString());

				}
			});
			my_car_casital_tv_14.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_14.getText()
							.toString());

				}
			});
			my_car_casital_tv_15.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_15.getText()
							.toString());

				}
			});
			my_car_casital_tv_16.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_16.getText()
							.toString());

				}
			});

			my_car_casital_tv_17.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_17.getText()
							.toString());

				}
			});
			my_car_casital_tv_21.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_21.getText()
							.toString());

				}
			});
			my_car_casital_tv_22.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_22.getText()
							.toString());

				}
			});
			my_car_casital_tv_23.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_23.getText()
							.toString());

				}
			});
			my_car_casital_tv_24.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_24.getText()
							.toString());

				}
			});
			my_car_casital_tv_25.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_25.getText()
							.toString());

				}
			});

			my_car_casital_tv_26.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_26.getText()
							.toString());

				}
			});
			my_car_casital_tv_27.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_27.getText()
							.toString());

				}
			});
			my_car_casital_tv_28.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_28.getText()
							.toString());

				}
			});
			my_car_casital_tv_31.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_31.getText()
							.toString());

				}
			});
			my_car_casital_tv_32.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_32.getText()
							.toString());

				}
			});
			my_car_casital_tv_33.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_33.getText()
							.toString());

				}
			});

			my_car_casital_tv_34.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_34.getText()
							.toString());

				}
			});
			my_car_casital_tv_35.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_35.getText()
							.toString());

				}
			});
			my_car_casital_tv_36.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_36.getText()
							.toString());

				}
			});
			my_car_casital_tv_37.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_37.getText()
							.toString());

				}
			});
			my_car_casital_tv_38.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_38.getText()
							.toString());

				}
			});
			my_car_casital_tv_39.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_39.getText()
							.toString());

				}
			});

			my_car_casital_tv_41.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_41.getText()
							.toString());

				}
			});
			my_car_casital_tv_42.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_42.getText()
							.toString());

				}
			});
			my_car_casital_tv_43.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_43.getText()
							.toString());

				}
			});
			my_car_casital_tv_44.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_44.getText()
							.toString());

				}
			});
			my_car_casital_tv_45.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_45.getText()
							.toString());

				}
			});
			my_car_casital_tv_46.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_46.getText()
							.toString());

				}
			});

			my_car_casital_tv_47.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_47.getText()
							.toString());

				}
			});
			my_car_casital_tv_48.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_48.getText()
							.toString());

				}
			});
			my_car_casital_tv_49.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_49.getText()
							.toString());

				}
			});
			my_car_casital_tv_50.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edit_bus_dialog.dismiss();
					edit_car_plate_tv.setText(my_car_casital_tv_50.getText()
							.toString());

				}
			});

		}

		if (!EditLovecar.this.isFinishing() && edit_bus_dialog != null
				&& !edit_bus_dialog.isShowing()) {
			edit_bus_dialog.show();
		}
	}

	private void doPost_editLoveBus(String car_id, String hudsn,
			String car_number, String enqine_number, String underpan_number) {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}
		resp = null;
		params = new AbRequestParams();

		params.put("access_token", Code.accessToken());
		params.put("userid", SystemInfo.getUserInfo().getUserid());
		params.put("car_id", car_id);
		params.put("hudsn", hudsn);
		params.put("car_number", car_number);
		params.put("engine_number", enqine_number);
		params.put("underpan_number", underpan_number);
		params.put("flag", "2");

		mAbHttpUtil.post(App.ZZD_REQUEST_CARHUD, params,
				new AbStringHttpResponseListener() {
					@Override
					public void onSuccess(int statusCode, String content) {
						resp = JSON.parseObject(content, RespEntity.class);
					}

					@Override
					public void onFailure(int statusCode, String content,
							Throwable error) {
					}

					@Override
					public void onStart() {
						// 显示进度框
						showProgressDialog();
					}

					@Override
					public void onFinish() {
						if (resp != null) {
							if (resp.getState() == 0) {
								showToast("修改车辆信息成功");
								EditLovecar.this.finish();
							} else if (resp.getState() == 1) {
								showToast(resp.getMsg());
							} else {
								showToast(resp.getMsg());
							}
							removeProgressDialog();
						} else {
							showToast("网络超时，请重试");
							removeProgressDialog();
							return;
						}
					}
				});
	}

}
